package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;

import org.primefaces.component.datatable.DataTable;


import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceResource;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ResourceDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class ServicesBean implements Serializable{
	private Map<String, Long> servicesMap = new HashMap<String, Long>();
	private Map<String, Long> modulesMap = new HashMap<String, Long>();
	ServiceDAO serviceDAO = new ServiceDAO();
	ModuleDAO moduleDAO = new ModuleDAO();
	ResourceDAO resourceDAO = new ResourceDAO();
	private List<Resource> assignResources = new ArrayList<Resource>();
	private List<Resource> availableResourcesInService = new ArrayList<Resource>();
	private Long selectedServiceId;
	private Long selectedModuleId;
	private Service selectedService;
	private Module selectedModule;
	private boolean detailsForService;
	private boolean addResourcesToService;
	private boolean availableResources;
	private DataTable assignedResourcesTable;
	private DataTable availableResourcesTable;
	private Resource[] selectedResources;
	private static final long serialVersionUID = 1L;
	private Service service=new Service();
	private Module module=new Module();
	private List<Resource> resourcesAvailableInService = new ArrayList<Resource>();
	private List<Resource> resourcesAvailableInModule = new ArrayList<Resource>();
	private Set<Resource> assignedResourcesSet = new HashSet<Resource>();
	private Set<Resource> availableResourcesSet = new HashSet<Resource>();
	
	
	
	public ServicesBean(){
		
		List<Module> modules = moduleDAO.getAllModules();
		for(Module module : modules)
		{
			modulesMap.put(module.getModuleName(), module.getModuleId());
		}
		
	}
	
	public void serviceChangeListener(AjaxBehaviorEvent e){
		
		selectedService = serviceDAO.getServiceById(selectedServiceId);
		System.out.println("NEW ID: " + selectedServiceId);
		if(selectedServiceId!=0)
		{
			setDetailsForService(true);
		}
		resourcesAvailableInService = resourceDAO.getResourcesByServiceId(selectedServiceId);
		System.out.println("Resources size: " + resourcesAvailableInService.size());
		
		availableResourcesSet.addAll( resourcesAvailableInService);
		availableResourcesInService.clear();
		availableResourcesInService.addAll(availableResourcesSet);	
		
	}
	public void moduleChangeListener(AjaxBehaviorEvent e)
	{	servicesMap.clear();
	if(selectedModuleId == null || selectedModuleId == 0)
	{
	
	}
	else
	{
		System.out.println("in moduleChangeListener ");
		//selectedModule = moduleDAO.getModuleById(selectedModuleId);
		//System.out.println("selectedModuleId: " + selectedModuleId);
		
		
		List<Service> services = serviceDAO.getServicesByModuleId(selectedModuleId);
		for(Service service : services)
		{
			servicesMap.put(service.getServiceName(), service.getServiceId());
		}
	}
		
	}
	public void addResourcesToServiceDisplay(ActionEvent e)
	{
			System.out.println("In addResourceToServiceDisplay");	
			setAddResourcesToService(true);
			System.out.println(addResourcesToService);
			
				assignedResourcesSet.addAll( new ArrayList<Resource>(Arrays.asList(selectedResources)));
				assignResources.clear();
				assignResources.addAll(assignedResourcesSet);	
		
	}
	public void availableResourcesToServiceDisplay(ActionEvent e)
	{
		setAvailableResources(true);
		resourcesAvailableInModule = resourceDAO.getResources(selectedModuleId);
	}
	public void updateService(ActionEvent e)
	{
		System.out.println("In update Service");
		
			
			// Create Service Resources for each assigned resources...
			for (Resource resource : assignResources) {
				ServiceResource serviceResource = new ServiceResource();
				serviceResource.setCreateDate(new Date());
				serviceResource.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				serviceResource.setStatus(CommonLookup.STATUS_ACTIVE);
				serviceResource.setResource(resource);
				if(resource.isSelected()){
					serviceResource.setStartPage(CommonLookup.START_PAGE_YES);
					selectedService.setStartPage(resource.getResourceName().toLowerCase());
				}	
				else
					serviceResource.setStartPage(CommonLookup.START_PAGE_NO);
				selectedService.getServiceResources().add(serviceResource);
			}

			serviceDAO.updateService(selectedService);
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, service
							.getServiceName()
							+ " service updated successfully.", ""));
			service = new Service();
		
	}
	
	public void removeService(ActionEvent e){
		e.getSource();
		UIData uiData = (UIData) e.getComponent().getParent().getParent();
		System.out.println("");
		System.out.println("Get Source:"+e.getSource());
		System.out.println("Get Source:"+uiData);
		if(uiData == assignedResourcesTable)
		{
			System.out.println("IN IF");
		Resource selectedResource = (Resource) assignedResourcesTable.getRowData();
		assignedResourcesSet.remove(selectedResource);
		assignResources.remove(selectedResource);	
		}else
		{
			System.out.println("IN ELSE");
			Resource selectedResourceAvailable = (Resource) availableResourcesTable.getRowData();
			availableResourcesSet.remove(selectedResourceAvailable);
			availableResourcesInService.remove(selectedResourceAvailable);			
		}
	}
	//GETTERS AND SETTERS
	public Service getSelectedService() {
		return selectedService;
	}

	public void setSelectedService(Service selectedService) {
		this.selectedService = selectedService;
	}
	public Map<String, Long> getServicesMap() {
		return servicesMap;
	}
	public void setServicesMap(Map<String, Long> servicesMap) {
		this.servicesMap = servicesMap;
	}

	public Long getSelectedServiceId() {
		return selectedServiceId;
	}

	public void setSelectedServiceId(Long selectedServiceId) {
		this.selectedServiceId = selectedServiceId;
	}

	public boolean isDetailsForService() {
		return detailsForService;
	}

	public void setDetailsForService(boolean detailsForService) {
		this.detailsForService = detailsForService;
	}

	public boolean isAddResourcesToService() {
		return addResourcesToService;
	}

	public void setAddResourcesToService(boolean addResourcesToService) {
		this.addResourcesToService = addResourcesToService;
	}

	public Map<String, Long> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, Long> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public Long getSelectedModuleId() {
		return selectedModuleId;
	}

	public void setSelectedModuleId(Long selectedModuleId) {
		this.selectedModuleId = selectedModuleId;
	}

	public Module getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(Module selectedModule) {
		this.selectedModule = selectedModule;
	}

	

	public Resource[] getSelectedResources() {
		return selectedResources;
	}

	public void setSelectedResources(Resource[] selectedResources) {
		this.selectedResources = selectedResources;
	}

	public List<Resource> getAssignResources() {
		return assignResources;
	}

	public void setAssignResources(List<Resource> assignResources) {
		this.assignResources = assignResources;
	}

	public boolean isAvailableResources() {
		return availableResources;
	}

	public void setAvailableResources(boolean availableResources) {
		this.availableResources = availableResources;
	}

	public DataTable getAssignedResourcesTable() {
		return assignedResourcesTable;
	}

	public void setAssignedResourcesTable(DataTable assignedResourcesTable) {
		this.assignedResourcesTable = assignedResourcesTable;
	}

	public Set<Resource> getAssignedResourcesSet() {
		return assignedResourcesSet;
	}

	public void setAssignedResourcesSet(Set<Resource> assignedResourcesSet) {
		this.assignedResourcesSet = assignedResourcesSet;
	}

	public DataTable getAvailableResourcesTable() {
		return availableResourcesTable;
	}

	public void setAvailableResourcesTable(DataTable availableResourcesTable) {
		this.availableResourcesTable = availableResourcesTable;
	}

	public List<Resource> getResourcesAvailableInService() {
		return resourcesAvailableInService;
	}

	public void setResourcesAvailableInService(
			List<Resource> resourcesAvailableInService) {
		this.resourcesAvailableInService = resourcesAvailableInService;
	}

	public List<Resource> getResourcesAvailableInModule() {
		return resourcesAvailableInModule;
	}

	public void setResourcesAvailableInModule(
			List<Resource> resourcesAvailableInModule) {
		this.resourcesAvailableInModule = resourcesAvailableInModule;
	}	
	
}
